package cn.elves.model.activity.service.impl;

import cn.elves.model.activity.entity.po.Activity;
import cn.elves.model.activity.entity.po.UserTakeActivity;
import cn.elves.model.activity.mapper.ActivityMapper;
import cn.elves.model.activity.service.ActivityService;
import cn.elves.model.activity.service.UserTakeActivityCountService;
import cn.elves.model.activity.service.UserTakeActivityService;
import cn.elves.model.activity.service.stateflow.impl.StateHandler;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author pc
 * @description 针对表【activity】的数据库操作Service实现
 * @createDate 2024-06-16 12:48:17
 */
@Service
public abstract class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
        implements ActivityService {

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private StateHandler stateHandler;

    @Resource
    private UserTakeActivityService userTakeActivityService;

    @Resource
    private UserTakeActivityCountService userTakeActivityCountService;


    @Override
    public void toDoingState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doDoing(id, activity.getState());
    }

    @Override
    public void toOpenState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doOpen(id, activity.getState());
    }

    @Override
    public void toCloseState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doClose(id, activity.getState());
    }

    @Override
    public void toRevokeState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doCheckRevoke(id, activity.getState());
    }

    @Override
    public void toRefuseState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doCheckRefuse(id, activity.getState());
    }

    @Override
    public void toPassState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doCheckPass(id, activity.getState());
    }

    @Override
    public void toArraignmentState(Long id) {
        Activity activity = activityMapper.selectById(id);
        if (activity == null) {
            throw new RuntimeException("当前活动不存在");
        }
        stateHandler.doArraignment(id, activity.getState());
    }

    @Override
    public UserTakeActivity queryActivityOrJoinActivityIfEffective(Long userId, Long activityId, Map<String, Object> matter) {
        Activity activity = activityMapper.selectEffectiveActivity(activityId);
        if (activity == null) {
            throw new RuntimeException("活动已过期");
        }
        UserTakeActivity userTakeActivityBill = userTakeActivityService.selectUnusedBillForUser(userId, activityId);
        if (userTakeActivityBill != null) {
            return userTakeActivityBill;
        }
        Boolean joinSuccess = userTakeActivityService.joinActivity(userId, activity,matter);
        if (!joinSuccess) {
            throw new RuntimeException("加入活动失败");
        }
        return userTakeActivityService.selectUnusedBillForUser(userId, activityId);
    }

    @Override
    public Boolean decreaseStockCount(Long id) {
        return activityMapper.decreaseStockCount(id);
    }
}




